package cn.wolfcode.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.domain.Appointment;
import cn.wolfcode.domain.Consumption;
import cn.wolfcode.mapper.ConsumptionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IConsumptionService;
import cn.wolfcode.util.UserContext;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ConsumptionServiceImpl implements IConsumptionService {

    @Autowired
    private ConsumptionMapper consumptionMapper;


    @Override
    public void save(Consumption consumption) {
        consumptionMapper.insert(consumption);
    }

    @Override
    public void delete(Long id) {
        consumptionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Consumption consumption) {
        consumptionMapper.updateByPrimaryKey(consumption);
    }

    @Override
    public Consumption get(Long id) {
        return consumptionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Consumption> listAll() {
        return consumptionMapper.selectAll();
    }

    @Override
    public PageInfo<Consumption> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize()); //对下一句sql进行自动分页
        List<Consumption> consumptions = consumptionMapper.selectForList(qo); //里面不需要自己写limit
        return new PageInfo<Consumption>(consumptions);
    }

    @Override
    public Long saveByAppointment(Appointment appointment) {
        //创建对象,把数据封装到对象中
        Consumption consumption = new Consumption();
        //生成消费的流水单号
        consumption.setCno(DateUtil.format(new Date(),"yyyyMMddHHmmss")+RandomUtil.randomNumbers(5));
        //设置状态为待结算
        consumption.setStatus(0);
        //设置客户名称
        consumption.setCustomerName(appointment.getContactName());
        //设置客户电话
        consumption.setCustomerTel(appointment.getContactTel());
        //预约流水单账户
        consumption.setAppointmentAno(appointment.getAno());
        //创建时间
        consumption.setCreateTime(new Date());
        //创建人
        consumption.setCreateUser(UserContext.getCurrentUser());
        //生成结算单
        consumptionMapper.insert(consumption);

        return consumption.getId();
    }

    @Override
    public Consumption selectByConsumptionCon(String cno) {
        return consumptionMapper.selectByConsumptionCon(cno);
    }

    @Override
    public void updateConsume(Long id, Integer status) {
        //设置创建时间
        Date date = new Date();
        //设置创建人
        Long payeeId = UserContext.getCurrentUser().getId();
        consumptionMapper.updateConsume(id,status,date,payeeId);
    }

    @Override
    public void updateAudit(Long id, Integer status) {
        //设置创建时间
        Date auditTime = new Date();
        //设置创建人
        Long auditorId = UserContext.getCurrentUser().getId();
        consumptionMapper.updateAudit(id,status,auditTime,auditorId);
    }
}

